package internal

import (
	"fmt"
	"reflect"
	"strings"
)

// toString 将一个对象转为 string
func toString(item interface{}) string {
	tp := reflect.TypeOf(item)
	switch tp.Kind() { // nolint
	case reflect.Bool:
		if ok, v := item.(bool); ok {
			if v {
				return "true"
			} else {
				return "false"
			}
		}
		panic("unexcept error")

	case reflect.Int:
		fallthrough
	case reflect.Int8:
		fallthrough
	case reflect.Int16:
		fallthrough
	case reflect.Int32:
		fallthrough
	case reflect.Int64:
		fallthrough
	case reflect.Uint:
		fallthrough
	case reflect.Uint8:
		fallthrough
	case reflect.Uint16:
		fallthrough
	case reflect.Uint32:
		fallthrough
	case reflect.Uint64:
		fallthrough
	case reflect.Float32:
		fallthrough
	case reflect.Float64:
		return fmt.Sprintf("%v", item)
	case reflect.String:
		return fmt.Sprintf("\"%v\"", item)
	}
	panic(fmt.Sprintf("unexcept value kind: %d, name: %s , value: %v", tp.Kind(), tp.Kind().String(), item))
}

// formatSql 格式化 sql
func formatSql(cmd string, args []interface{}) string {
	for _, v := range args {
		str := toString(v)
		cmd = strings.Replace(cmd, "?", str, 1)
	}
	return cmd
}
